//
//  YYDateTimePickerView.swift
//  DatePickerView
//
//  Created by 蓓蕾 on 2022/3/22.
//

import UIKit

class YYDateTimePickerView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
    
    enum YYDatePickerMode:Int {
        // ----- 以下4种是系统样式（兼容国际化日期格式） -----
        /** 【yyyy-MM-dd】UIDatePickerModeDate（美式日期：MM-dd-yyyy；英式日期：dd-MM-yyyy）*/
        case YYDatePickerModeDate
        /** 【yyyy-MM-dd HH:mm】 UIDatePickerModeDateAndTime */
        case YYDatePickerModeDateAndTime
        /** 【HH:mm】UIDatePickerModeTime */
        case YYDatePickerModeTime
        /** 【HH:mm】UIDatePickerModeCountDownTimer */
        case YYDatePickerModeCountDownTimer
        
        // ----- 以下11种是自定义样式 -----
        /** 【yyyy-MM-dd HH:mm:ss】年月日时分秒 */
        case YYDatePickerModeYMDHMS
        /** 【yyyy-MM-dd HH:mm】年月日时分 */
        case YYDatePickerModeYMDHM
        /** 【yyyy-MM-dd HH】年月日时 */
        case YYDatePickerModeYMDH
        /** 【MM-dd HH:mm】月日时分 */
        case YYDatePickerModeMDHM
        /** 【yyyy-MM-dd】年月日（兼容国际化日期：dd-MM-yyyy）*/
        case YYDatePickerModeYMD
        /** 【yyyy-MM】年月（兼容国际化日期：MM-yyyy）*/
        case YYDatePickerModeYM
        /** 【yyyy】年 */
        case YYDatePickerModeY
        /** 【MM-dd】月日 */
        case YYDatePickerModeMD
        /** 【HH:mm:ss】时分秒 */
        case YYDatePickerModeHMS
        /** 【HH:mm】时分 */
        case YYDatePickerModeHM
        /** 【mm:ss】分秒 */
        case YYDatePickerModeMS
    }
    
//    enum YYShowUnitType:Int {
//        /** 日期单位显示全部行（默认）*/
//        case YYShowUnitTypeAll
//        /** 日期单位仅显示中间行 */
//        case YYShowUnitTypeOnlyCenter
//        /** 日期单位不显示（隐藏日期单位）*/
//        case YYShowUnitTypeNone
//    }
    
    enum YYShowUnitType {
        /** 日期单位显示全部行（默认）*/
        case YYShowUnitTypeAll
        /** 日期单位仅显示中间行 */
        case YYShowUnitTypeOnlyCenter
        /** 日期单位不显示（隐藏日期单位）*/
        case YYShowUnitTypeNone
    }
    
    typealias YYDateResultBlock=(NSDate, String)->Void
    var selectResultBlock: YYDateResultBlock?
    
    let systemBlackColr = UIColor(hex: 0x424243)//系统常用的黑色
    let systemgrayColor = UIColor(hex: 0xb2b2b2)//系统常用的灰色
    
    var CancelReturnBlock: (() -> Void)? //取消按钮的回调
//    var SuccessReturnBlock: ((_ date:String) -> Void)?//选择的回调
    
    var bgView : UIView?
    private lazy var titleView = UILabel()
    private lazy var cancelButton = UIButton()
    private lazy var confirmButton = UIButton()
    var lineView = UIView()//一条横线
    var pickerView = UIPickerView()
    
    var BorderHeight = 400
    var PickerColumnNumber = 6
    private var currentDatePickerMode = YYDatePickerMode.YYDatePickerModeYMDHMS
    private var currentShowUnitType = YYShowUnitType.YYShowUnitTypeAll
    private var currentDateFormat = "yyyy-MM-dd HH:mm:ss"
    
    private var resultSelectDateStringValue : String!
    private var resultSelectDateValue : NSDate!
    /// 默认选中日期
    private var initialDateString = ""
    
    //数据相关
    var yearRange = 30 + 1000//年的范围
    var dayRange = 0 //
    var startYear = 1970//起始年份
    
    
    var selectedYear = 0
    var selectedMonth = 0
    var selectedDay = 0
    var selectedHour = 0
    var selectedMinute = 0
    var selectedSecond = 0
    
//    var showUnitType:YYShowUnitType = .YYShowUnitTypeAll {
//
//    }
    
    var showUnitType:YYShowUnitType = .YYShowUnitTypeAll {
        didSet {
            currentShowUnitType = showUnitType
            pickerView.reloadAllComponents()
        }
    }
    
    var returnString:String  {
        get{
            let selectedMonthFormat = String(format:"%.2d",selectedMonth)
            let selectedDayFormat = String(format:"%.2d",selectedDay)
            let selectedHourFormat = String(format:"%.2d",selectedHour)
            let selectedMinuteFormat = String(format:"%.2d",selectedMinute)
            let selectedSecondFormat = String(format:"%.2d",selectedSecond)
            
            let dateFormatter = DateFormatter()
            // 根据时区名称创建
//            let zone:NSTimeZone = NSTimeZone(name: "America/Chicago")!
            // 根据时区缩写创建
            let zone:NSTimeZone = NSTimeZone(abbreviation: "UTC")!//
//            let zone:NSTimeZone = NSTimeZone(abbreviation: "EST")!
            // 根据零时区的秒数偏移量创建，+28800 为 GMT+8，即北京时间(+8)
//            let zone:NSTimeZone = NSTimeZone(forSecondsFromGMT: +28800)
            dateFormatter.timeZone = zone as TimeZone
            switch currentDatePickerMode {
            case .YYDatePickerModeDate:
                resultSelectDateStringValue = "\(selectedYear)-\(selectedMonthFormat)-\(selectedDayFormat)"
//                dateFormatter.dateFormat = "yyyy-MM-dd"
            case .YYDatePickerModeDateAndTime:
                resultSelectDateStringValue = "\(selectedYear)-\(selectedMonthFormat)-\(selectedDayFormat) \(selectedHourFormat):\(selectedMinuteFormat):\(selectedSecondFormat)"
//                dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            case .YYDatePickerModeTime:
                resultSelectDateStringValue = "\(selectedHourFormat):\(selectedMinuteFormat):\(selectedSecondFormat)"
//                dateFormatter.dateFormat = "HH:mm:ss"
            case .YYDatePickerModeCountDownTimer:
                resultSelectDateStringValue = "\(selectedHourFormat):\(selectedMinuteFormat):\(selectedSecondFormat)"
//                dateFormatter.dateFormat = "HH:mm:ss"
            case .YYDatePickerModeYMDHMS:
                resultSelectDateStringValue = "\(selectedYear)-\(selectedMonthFormat)-\(selectedDayFormat) \(selectedHourFormat):\(selectedMinuteFormat):\(selectedSecondFormat)"
//                dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            case .YYDatePickerModeYMDHM:
                resultSelectDateStringValue = "\(selectedYear)-\(selectedMonthFormat)-\(selectedDayFormat) \(selectedHourFormat):\(selectedMinuteFormat)"
//                dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
            case .YYDatePickerModeYMDH:
                resultSelectDateStringValue = "\(selectedYear)-\(selectedMonthFormat)-\(selectedDayFormat) \(selectedHourFormat)"
//                dateFormatter.dateFormat = "yyyy-MM-dd HH"
            case .YYDatePickerModeMDHM:
                resultSelectDateStringValue = "\(selectedMonthFormat)-\(selectedDayFormat) \(selectedHourFormat):\(selectedMinuteFormat)"
//                dateFormatter.dateFormat = "MM-dd HH:mm"
            case .YYDatePickerModeYMD:
                resultSelectDateStringValue = "\(selectedYear)-\(selectedMonthFormat)-\(selectedDayFormat)"
//                dateFormatter.dateFormat = "yyyy-MM-dd"
            case .YYDatePickerModeYM:
                resultSelectDateStringValue = "\(selectedYear)-\(selectedMonthFormat)"
//                dateFormatter.dateFormat = "yyyy-MM"
            case .YYDatePickerModeY:
                resultSelectDateStringValue = "\(selectedYear)"
//                dateFormatter.dateFormat = "yyyy"
            case .YYDatePickerModeMD:
                resultSelectDateStringValue = "\(selectedMonthFormat)-\(selectedDayFormat)"
//                dateFormatter.dateFormat = "MM-dd"
            case .YYDatePickerModeHMS:
                resultSelectDateStringValue = "\(selectedHourFormat):\(selectedMinuteFormat):\(selectedSecondFormat)"
//                dateFormatter.dateFormat = "HH:mm:ss"
            case .YYDatePickerModeHM:
                resultSelectDateStringValue = "\(selectedHourFormat):\(selectedMinuteFormat)"
//                dateFormatter.dateFormat = "HH:mm"
            case .YYDatePickerModeMS:
                resultSelectDateStringValue = "\(selectedMinuteFormat):\(selectedSecondFormat)"
//                dateFormatter.dateFormat = "mm:ss"
            }
            print("currentDateFormat = \(currentDateFormat)")
            dateFormatter.dateFormat = currentDateFormat
            
            
            let fullDateFormatter = DateFormatter()
            fullDateFormatter.timeZone = zone as TimeZone
            fullDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            let fullDateString:String = "\(selectedYear)-\(selectedMonthFormat)-\(selectedDayFormat) \(selectedHourFormat):\(selectedMinuteFormat):\(selectedSecondFormat)"
            resultSelectDateValue = fullDateFormatter.date(from: fullDateString) as NSDate?
            
            return resultSelectDateStringValue
        }
    } //返回的时间字符串
    
    static let shared = YYDateTimePickerView()
    
    private override init(frame: CGRect) {
        super.init(frame: frame)
        print("init 方法")
        initUI()
//        initData()
    }
    
    override func copy() -> Any {
        print("copy 方法")
        return self // SingletonClass.shared
    }
    
    override func mutableCopy() -> Any {
        print("mutableCopy 方法")
        return self // SingletonClass.shared
    }
    
    func reset() {
        print("reset 方法")
    }
    
    private func initUI(){
        let boundsSizeWidth:CGFloat = UIScreen.main.bounds.width
        let boundsSizeHeight:CGFloat = UIScreen.main.bounds.height
        self.frame = CGRect(x: 0, y: 0, width: boundsSizeWidth, height: boundsSizeHeight)
//        self.backgroundColor = UIColor.white
//        if bgView == nil {
//            bgView = UIView(frame: UIScreen.main.bounds)
//        }
//        if bgView!.superview == nil {
//            getWindow().addSubview(bgView!)
//        }
        
        pickerView.frame = CGRect(x: 0, y: Int(boundsSizeHeight) - BorderHeight, width: Int(boundsSizeWidth), height: BorderHeight)
        pickerView.delegate = self
        pickerView.dataSource = self
        self.addSubview(pickerView)
        
        
        let topBgView = UIView()
        self.addSubview(topBgView)
        topBgView.frame = CGRect(x: 0, y: pickerView.frame.minY + 5, width: boundsSizeWidth, height: 40)
        topBgView.backgroundColor = .white
        
        
        lineView.frame = CGRect(x: 15, y: topBgView.bounds.height - 1, width: topBgView.bounds.width - 30, height: 1)
        lineView.backgroundColor = UIColor.systemGray
        topBgView.addSubview(lineView)
        
        cancelButton.frame = CGRect(x: 15, y: (topBgView.bounds.height - 20) / 2, width: 50, height: 20)
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.setTitleColor(UIColor.system, for: .normal)
        cancelButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        cancelButton.tag = 101
        cancelButton.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        topBgView.addSubview(cancelButton)
        
        confirmButton.frame = CGRect(x: boundsSizeWidth - 50 - 15, y: cancelButton.frame.minY, width: 50, height: 20)
        confirmButton.setTitle("确定", for: .normal)
        confirmButton.setTitleColor(UIColor.system, for: .normal)
        confirmButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        confirmButton.tag = 102
        confirmButton.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        topBgView.addSubview(confirmButton)
        
        let titleViewX = cancelButton.frame.maxX + 15
        titleView.frame = CGRect(x: titleViewX, y: (topBgView.bounds.height - 20) / 2, width: confirmButton.frame.minX - 15 - titleViewX, height: 20)
        titleView.textColor = systemBlackColr
        titleView.textAlignment = .center
        titleView.font = UIFont.systemFont(ofSize: 15)
        titleView.text = "选择时间"
        topBgView.addSubview(titleView)
        
        
    }
    
    //MARK: 初始化数据
    private func initData()  {
        
        let  calendar0 = Calendar.init(identifier: .gregorian)//公历
        var comps = DateComponents()//一个封装了具体年月日、时秒分、周、季度等的类
        let unitFlags:Set<Calendar.Component> = [.year , .month , .day , .hour , .minute , .second]
        
        if initialDateString.count > 0 {
            print("有默认日期")
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = currentDateFormat
            let date = dateFormatter.date(from: initialDateString)
            comps = calendar0.dateComponents(unitFlags, from: date ?? Date())
        } else {
            print("没有默认日期")
            comps = calendar0.dateComponents(unitFlags, from: Date())
        }
        
        
        yearRange = 30 + 1000
        
        if currentDatePickerMode == .YYDatePickerModeMDHM || currentDatePickerMode == .YYDatePickerModeMD {
            var tempComps = DateComponents()//一个封装了具体年月日、时秒分、周、季度等的类
            tempComps = calendar0.dateComponents(unitFlags, from: Date())
            selectedYear = tempComps.year!
        } else {
            selectedYear = comps.year!
        }
        
        selectedMonth = comps.month!
        selectedDay = comps.day!
        selectedHour = comps.hour!
        selectedMinute = comps.minute!
        selectedSecond = comps.second!
        startYear = startYear > 0 ? startYear : comps.year! - 100
        dayRange = isAllDay(year: selectedYear, month: selectedMonth)
        
        print("selectedYear = \(selectedYear)")
        print("selectedMonth = \(selectedMonth)")
        print("selectedDay = \(selectedDay)")
        print("selectedHour = \(selectedHour)")
        print("selectedMinute = \(selectedMinute)")
        print("selectedSecond = \(selectedSecond)")
        
        switch currentDatePickerMode {
        case .YYDatePickerModeDate:
            PickerColumnNumber = 3
            self.pickerView.selectRow(selectedYear - startYear, inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedMonth - 1, inComponent: 1, animated: true)
            self.pickerView.selectRow(selectedDay - 1, inComponent: 2, animated: true)
        case .YYDatePickerModeDateAndTime:
            PickerColumnNumber = 6
            self.pickerView.selectRow(selectedYear - startYear, inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedMonth - 1, inComponent: 1, animated: true)
            self.pickerView.selectRow(selectedDay - 1, inComponent: 2, animated: true)
            self.pickerView.selectRow(selectedHour , inComponent: 3, animated: true)
            self.pickerView.selectRow(selectedMinute , inComponent: 4, animated: true)
            self.pickerView.selectRow(selectedSecond , inComponent: 5, animated: true)
        case .YYDatePickerModeTime:
            PickerColumnNumber = 3
            self.pickerView.selectRow(selectedHour, inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedMinute, inComponent: 1, animated: true)
            self.pickerView.selectRow(selectedSecond, inComponent: 2, animated: true)
        case .YYDatePickerModeCountDownTimer:
            PickerColumnNumber = 3
            self.pickerView.selectRow(selectedHour, inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedMinute, inComponent: 1, animated: true)
            self.pickerView.selectRow(selectedSecond, inComponent: 2, animated: true)
        case .YYDatePickerModeYMDHMS:
            PickerColumnNumber = 6
            self.pickerView.selectRow(selectedYear - startYear, inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedMonth - 1, inComponent: 1, animated: true)
            self.pickerView.selectRow(selectedDay - 1, inComponent: 2, animated: true)
            self.pickerView.selectRow(selectedHour , inComponent: 3, animated: true)
            self.pickerView.selectRow(selectedMinute , inComponent: 4, animated: true)
            self.pickerView.selectRow(selectedSecond , inComponent: 5, animated: true)
        case .YYDatePickerModeYMDHM:
            PickerColumnNumber = 5
            self.pickerView.selectRow(selectedYear - startYear, inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedMonth - 1, inComponent: 1, animated: true)
            self.pickerView.selectRow(selectedDay - 1, inComponent: 2, animated: true)
            self.pickerView.selectRow(selectedHour , inComponent: 3, animated: true)
            self.pickerView.selectRow(selectedMinute , inComponent: 4, animated: true)
        case .YYDatePickerModeYMDH:
            PickerColumnNumber = 4
            self.pickerView.selectRow(selectedYear - startYear, inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedMonth - 1, inComponent: 1, animated: true)
            self.pickerView.selectRow(selectedDay - 1, inComponent: 2, animated: true)
            self.pickerView.selectRow(selectedHour , inComponent: 3, animated: true)
        case .YYDatePickerModeMDHM:
            PickerColumnNumber = 4
            self.pickerView.selectRow(selectedMonth - 1, inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedDay - 1, inComponent: 1, animated: true)
            self.pickerView.selectRow(selectedHour, inComponent: 2, animated: true)
            self.pickerView.selectRow(selectedMinute , inComponent: 3, animated: true)
        case .YYDatePickerModeYMD:
            PickerColumnNumber = 3
            self.pickerView.selectRow(selectedYear - startYear, inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedMonth - 1, inComponent: 1, animated: true)
            self.pickerView.selectRow(selectedDay - 1, inComponent: 2, animated: true)
        case .YYDatePickerModeYM:
            PickerColumnNumber = 2
            self.pickerView.selectRow(selectedYear - startYear, inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedMonth - 1, inComponent: 1, animated: true)
        case .YYDatePickerModeY:
            PickerColumnNumber = 1
            self.pickerView.selectRow(selectedYear - startYear, inComponent: 0, animated: true)
        case .YYDatePickerModeMD:
            PickerColumnNumber = 2
            self.pickerView.selectRow(selectedMonth - 1, inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedDay - 1, inComponent: 1, animated: true)
        case .YYDatePickerModeHMS:
            PickerColumnNumber = 3
            self.pickerView.selectRow(selectedHour , inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedMinute , inComponent: 1, animated: true)
            self.pickerView.selectRow(selectedSecond , inComponent: 2, animated: true)
        case .YYDatePickerModeHM:
            PickerColumnNumber = 2
            self.pickerView.selectRow(selectedHour , inComponent: 0, animated: true)
            self.pickerView.selectRow(selectedMinute , inComponent: 1, animated: true)
        case .YYDatePickerModeMS:
            PickerColumnNumber = 2
            self.pickerView.selectRow(selectedMinute , inComponent: 1, animated: true)
            self.pickerView.selectRow(selectedSecond , inComponent: 2, animated: true)
        }
        self.pickerView.reloadAllComponents()
    }
    
    //MARK: 计算每个月有多少天
    func isAllDay(year:Int, month:Int) -> Int {
        var day:Int = 0
        switch(month)
        {
        case 1,3,5,7,8,10,12:
            day = 31
        case 4,6,9,11:
            day = 30
        case 2:
            if(((year%4==0)&&(year%100==0))||(year%400==0))
            {
                day=29
            }
            else
            {
                day=28;
            }
        default:
            break;
        }
        return day;
    }
    //MARK: 按钮的响应事件
    @objc private func buttonClick(btn:UIButton){
        let tag:Int = btn.tag
        switch tag {
        case 101:
            dismissDatePickerView()
        case 102:
            dismissDatePickerView()
            if self.selectResultBlock != nil {
                let resultString:String = returnString
                let date:NSDate = resultSelectDateValue
                self.selectResultBlock!(date,resultString)
            }
        default:
            break
        }
    }
    
    //MARK: UIPickerViewDataSource
    
    //返回UIPickerView当前的列数
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return PickerColumnNumber
    }
    //确定每一列返回的东西
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        switch currentDatePickerMode {
        case .YYDatePickerModeDate:
            switch component {
            case 0:
                return yearRange
            case 1:
                return 12
            case 2:
                return dayRange
            default:
                return 0
            }
        case .YYDatePickerModeDateAndTime:
            switch component {
            case 0:
                return 24
            case 1:
                return 60
            case 2:
                return 60
            default:
                return 0
            }
        case .YYDatePickerModeTime:
            switch component {
            case 0:
                return 24
            case 1:
                return 60
            case 2:
                return 60
            default:
                return 0
            }
        case .YYDatePickerModeCountDownTimer:
            switch component {
            case 0:
                return 24
            case 1:
                return 60
            case 2:
                return 60
            default:
                return 0
            }
        case .YYDatePickerModeYMDHMS:
            switch component {
            case 0:
                return yearRange
            case 1:
                return 12
            case 2:
                return dayRange
            case 3:
                return 24
            case 4:
                return 60
            case 5:
                return 60
            default:
                return 0
            }
        case .YYDatePickerModeYMDHM:
            switch component {
            case 0:
                return yearRange
            case 1:
                return 12
            case 2:
                return dayRange
            case 3:
                return 24
            case 4:
                return 60
            default:
                return 0
            }
        case .YYDatePickerModeYMDH:
            switch component {
            case 0:
                return yearRange
            case 1:
                return 12
            case 2:
                return dayRange
            case 3:
                return 24
            default:
                return 0
            }
        case .YYDatePickerModeMDHM:
            switch component {
            case 0:
                return 12
            case 1:
                return dayRange
            case 2:
                return 24
            case 3:
                return 60
            default:
                return 0
            }
        case .YYDatePickerModeYMD:
            switch component {
            case 0:
                return yearRange
            case 1:
                return 12
            case 2:
                return dayRange
            default:
                return 0
            }
        case .YYDatePickerModeYM:
            switch component {
            case 0:
                return yearRange
            case 1:
                return 12
            default:
                return 0
            }
        case .YYDatePickerModeY:
            switch component {
            case 0:
                return yearRange
            default:
                return 0
            }
        case .YYDatePickerModeMD:
            switch component {
            case 0:
                return 12
            case 1:
                return dayRange
            default:
                return 0
            }
        case .YYDatePickerModeHMS:
            switch component {
            case 0:
                return 24
            case 1:
                return 60
            case 2:
                return 60
            default:
                return 0
            }
        case .YYDatePickerModeHM:
            switch component {
            case 0:
                return 24
            case 1:
                return 60
            default:
                return 0
            }
        case .YYDatePickerModeMS:
            switch component {
            case 0:
                return 60
            case 1:
                return 60
            default:
                return 0
            }
        }
        
        
        
        
        
        
//        switch component {
//        case 0:
//            return yearRange
//        case 1:
//            return 12
//        case 2:
//            return dayRange
//        case 3:
//            return 24
//        case 4:
//            return 60
//        case 5:
//            return 60
//        default:
//            return 0
//        }
    }
    //MARK: UIPickerViewDelegate
    
    //返回一个视图，用来设置pickerView的每行显示的内容。
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let boundsSizeWidth = UIScreen.main.bounds.width
        let labelView = UILabel(frame: CGRect(x: boundsSizeWidth * CGFloat(component) / 6, y: 0, width: boundsSizeWidth / 6, height: 30))
        labelView.font = UIFont.systemFont(ofSize: 15)
        labelView.tag = component*100+row
        labelView.textAlignment = .center
        
        var year = "年"
        var month = "月"
        var day = "日"
        var hour = "时"
        var minute = "分"
        var second = "秒"
        
        
        switch currentShowUnitType {
        case .YYShowUnitTypeAll:
            year = "年"
            month = "月"
            day = "日"
            hour = "时"
            minute = "分"
            second = "秒"
        case .YYShowUnitTypeOnlyCenter:
            break
        case .YYShowUnitTypeNone:
            year = ""
            month = ""
            day = ""
            hour = ""
            minute = ""
            second = ""
        }
        
        
        switch currentDatePickerMode {
        case .YYDatePickerModeDate:
            switch component {
            case 0:
                labelView.frame=CGRect(x:5, y:0,width:boundsSizeWidth/4.0, height:30);
                labelView.text="\(self.startYear + row)\(year)";
            case 1:
                labelView.frame=CGRect(x:boundsSizeWidth/4.0, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)\(month)";
            case 2:
                labelView.frame=CGRect(x:boundsSizeWidth*3/8, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)\(day)";
            default:
                break
            }
        case .YYDatePickerModeDateAndTime:
            switch component {
            case 0:
                labelView.textAlignment = .right
                labelView.text="\(row )时";
            case 1:
                labelView.textAlignment = .right
                labelView.text="\(row )分";
            case 2:
                labelView.textAlignment = .right
                labelView.frame=CGRect(x:boundsSizeWidth/6, y:0,width:boundsSizeWidth/6.0 - 5, height:30)
                labelView.text="\(row )秒"
            default:
                break
            }
        case .YYDatePickerModeTime:
            switch component {
            case 0:
                labelView.textAlignment = .right
                labelView.text="\(row )时";
            case 1:
                labelView.textAlignment = .right
                labelView.text="\(row )分";
            case 2:
                labelView.textAlignment = .right
                labelView.frame=CGRect(x:boundsSizeWidth/6, y:0,width:boundsSizeWidth/6.0 - 5, height:30)
                labelView.text="\(row )秒"
            default:
                labelView.text="\(row )秒"
            }
        case .YYDatePickerModeCountDownTimer:
            switch component {
            case 0:
                labelView.textAlignment = .right
                labelView.text="\(row )时";
            case 1:
                labelView.textAlignment = .right
                labelView.text="\(row )分";
            case 2:
                labelView.textAlignment = .right
                labelView.frame=CGRect(x:boundsSizeWidth/6, y:0,width:boundsSizeWidth/6.0 - 5, height:30)
                labelView.text="\(row )秒"
            default:
                break
            }
        case .YYDatePickerModeYMDHMS:
            switch component {
            case 0:
                labelView.frame=CGRect(x:5, y:0,width:boundsSizeWidth/4.0, height:30);
                labelView.text="\(self.startYear + row)年";
            case 1:
                labelView.frame=CGRect(x:boundsSizeWidth/4.0, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)月";
            case 2:
                labelView.frame=CGRect(x:boundsSizeWidth*3/8, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)日";
            case 3:
                labelView.textAlignment = .right
                labelView.text="\(row )时";
            case 4:
                labelView.textAlignment = .right
                labelView.text="\(row )分";
            case 5:
                labelView.textAlignment = .right
                labelView.frame=CGRect(x:boundsSizeWidth/6, y:0,width:boundsSizeWidth/6.0 - 5, height:30)
                labelView.text="\(row )秒"
            default:
                break
            }
        case .YYDatePickerModeYMDHM:
            switch component {
            case 0:
                labelView.frame=CGRect(x:5, y:0,width:boundsSizeWidth/4.0, height:30);
                labelView.text="\(self.startYear + row)年";
            case 1:
                labelView.frame=CGRect(x:boundsSizeWidth/4.0, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)月";
            case 2:
                labelView.frame=CGRect(x:boundsSizeWidth*3/8, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)日";
            case 3:
                labelView.textAlignment = .right
                labelView.text="\(row )时";
            case 4:
                labelView.textAlignment = .right
                labelView.text="\(row )分";
            default:
                break
            }
        case .YYDatePickerModeYMDH:
            switch component {
            case 0:
                labelView.frame=CGRect(x:5, y:0,width:boundsSizeWidth/4.0, height:30);
                labelView.text="\(self.startYear + row)年";
            case 1:
                labelView.frame=CGRect(x:boundsSizeWidth/4.0, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)月";
            case 2:
                labelView.frame=CGRect(x:boundsSizeWidth*3/8, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)日";
            case 3:
                labelView.textAlignment = .right
                labelView.text="\(row )时";
            default:
                break
            }
        case .YYDatePickerModeMDHM:
            switch component {
            case 0:
                labelView.frame=CGRect(x:boundsSizeWidth/4.0, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)月";
            case 1:
                labelView.frame=CGRect(x:boundsSizeWidth*3/8, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)日";
            case 2:
                labelView.textAlignment = .right
                labelView.text="\(row )时";
            case 3:
                labelView.textAlignment = .right
                labelView.text="\(row )分";
            default:
                break
            }
        case .YYDatePickerModeYMD:
            switch component {
            case 0:
                labelView.frame=CGRect(x:5, y:0,width:boundsSizeWidth/4.0, height:30);
                labelView.text="\(self.startYear + row)年";
            case 1:
                labelView.frame=CGRect(x:boundsSizeWidth/4.0, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)月";
            case 2:
                labelView.frame=CGRect(x:boundsSizeWidth*3/8, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)日";
            default:
                break
            }
        case .YYDatePickerModeYM:
            switch component {
            case 0:
                labelView.frame=CGRect(x:5, y:0,width:boundsSizeWidth/4.0, height:30);
                labelView.text="\(self.startYear + row)年";
            case 1:
                labelView.frame=CGRect(x:boundsSizeWidth/4.0, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)月";
            default:
                break
            }
        case .YYDatePickerModeY:
            switch component {
            case 0:
                labelView.frame=CGRect(x:5, y:0,width:boundsSizeWidth/4.0, height:30);
                labelView.text="\(self.startYear + row)年";
            default:
                break
            }
        case .YYDatePickerModeMD:
            switch component {
            case 0:
                labelView.frame=CGRect(x:boundsSizeWidth/4.0, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)月";
            case 1:
                labelView.frame=CGRect(x:boundsSizeWidth*3/8, y:0,width:boundsSizeWidth/8.0, height:30)
                labelView.text="\(row + 1)日";
            default:
                break
            }
        case .YYDatePickerModeHMS:
            switch component {
            case 0:
                labelView.textAlignment = .right
                labelView.text="\(row )时";
            case 1:
                labelView.textAlignment = .right
                labelView.text="\(row )分";
            case 2:
                labelView.textAlignment = .right
                labelView.frame=CGRect(x:boundsSizeWidth/6, y:0,width:boundsSizeWidth/6.0 - 5, height:30)
                labelView.text="\(row )秒"
            default:
                break
            }
        case .YYDatePickerModeHM:
            switch component {
            case 0:
                labelView.textAlignment = .right
                labelView.text="\(row )时";
            case 1:
                labelView.textAlignment = .right
                labelView.text="\(row )分";
            default:
                break
            }
        case .YYDatePickerModeMS:
            switch component {
            case 0:
                labelView.textAlignment = .right
                labelView.text="\(row )分";
            case 1:
                labelView.textAlignment = .right
                labelView.frame=CGRect(x:boundsSizeWidth/6, y:0,width:boundsSizeWidth/6.0 - 5, height:30)
                labelView.text="\(row )秒"
            default:
                break
            }
        }
        return labelView
        
        
        
//        switch component {
//        case 0:
//            labelView.frame=CGRect(x:5, y:0,width:boundsSizeWidth/4.0, height:30);
//            labelView.text="\(self.startYear + row)年";
//        case 1:
//            labelView.frame=CGRect(x:boundsSizeWidth/4.0, y:0,width:boundsSizeWidth/8.0, height:30)
//            labelView.text="\(row + 1)月";
//        case 2:
//            labelView.frame=CGRect(x:boundsSizeWidth*3/8, y:0,width:boundsSizeWidth/8.0, height:30)
//            labelView.text="\(row + 1)日";
//        case 3:
//            labelView.textAlignment = .right
//            labelView.text="\(row )时";
//        case 4:
//            labelView.textAlignment = .right
//            labelView.text="\(row )分";
//        case 5:
//            labelView.textAlignment = .right
//            labelView.frame=CGRect(x:boundsSizeWidth/6, y:0,width:boundsSizeWidth/6.0 - 5, height:30)
//            labelView.text="\(row )秒"
//        default:
//            labelView.text="\(row )秒"
//        }
//        return labelView
    }
    
    
    //当点击UIPickerView的某一列中某一行的时候，就会调用这个方法
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        switch currentDatePickerMode {
        case .YYDatePickerModeDate:
            switch component {
            case 0://年
                self.selectedYear = self.startYear + row
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(2)
            case 1://月
                self.selectedMonth =  row + 1
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(2)
            case 2://日
                selectedDay = row + 1
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeDateAndTime:
            switch component {
            case 0://时
                selectedHour = row
            case 1://分
                selectedMinute = row
            case 2://秒
                selectedSecond = row
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeTime:
            switch component {
            case 0://时
                selectedHour = row
            case 1://分
                selectedMinute = row
            case 2://秒
                selectedSecond = row
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeCountDownTimer:
            switch component {
            case 0://时
                selectedHour = row
            case 1://分
                selectedMinute = row
            case 2://秒
                selectedSecond = row
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeYMDHMS:
            switch component {
            case 0://年
                self.selectedYear = self.startYear + row
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(2)
            case 1://月
                self.selectedMonth =  row + 1
                
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(2)
            case 2://日
                selectedDay = row + 1
            case 3://时
                selectedHour = row
            case 4://分
                selectedMinute = row
            case 5://秒
                selectedSecond = row
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeYMDHM:
            switch component {
            case 0://年
                self.selectedYear = self.startYear + row
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(2)
            case 1://月
                self.selectedMonth =  row + 1
                
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(2)
            case 2://日
                selectedDay = row + 1
            case 3://时
                selectedHour = row
            case 4://分
                selectedMinute = row
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeYMDH:
            switch component {
            case 0://年
                self.selectedYear = self.startYear + row
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(2)
            case 1://月
                self.selectedMonth =  row + 1
                
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(2)
            case 2://日
                selectedDay = row + 1
            case 3://时
                selectedHour = row
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeMDHM:
            switch component {
            case 0://月
                self.selectedMonth =  row + 1
                
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(1)
            case 1://日
                selectedDay = row + 1
            case 2://时
                selectedHour = row
            case 3://分
                selectedMinute = row
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeYMD:
            switch component {
            case 0://年
                self.selectedYear = self.startYear + row
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(2)
            case 1://月
                self.selectedMonth =  row + 1
                
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(2)
            case 2://日
                selectedDay = row + 1
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeYM:
            switch component {
            case 0://年
                self.selectedYear = self.startYear + row
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
//                self.pickerView.reloadComponent(2)
            case 1://月
                self.selectedMonth =  row + 1
                
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
//                self.pickerView.reloadComponent(2)
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeY:
            switch component {
            case 0://年
                self.selectedYear = self.startYear + row
                self.dayRange = self.isAllDay(year: self.startYear, month: self.selectedMonth)
//                self.pickerView.reloadComponent(2)
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeMD:
            switch component {
            case 0://月
                self.selectedMonth =  row + 1
                
                self.dayRange = self.isAllDay(year: self.selectedYear, month: self.selectedMonth)
                self.pickerView.reloadComponent(1)
            case 1://日
                selectedDay = row + 1
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeHMS:
            switch component {
            case 0://时
                selectedHour = row
            case 1://分
                selectedMinute = row
            case 2://秒
                selectedSecond = row
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeHM:
            switch component {
            case 0://时
                selectedHour = row
            case 1://分
                selectedMinute = row
            default:
                selectedSecond = row
            }
        case .YYDatePickerModeMS:
            switch component {
            case 0://分
                selectedMinute = row
            case 1://秒
                selectedSecond = row
            default:
                selectedSecond = row
            }
        }
        
        
        
        
//        switch component {
//        case 0://年
//            self.selectedYear = self.startYear + row
//            self.dayRange = self.isAllDay(year: self.startYear, month: self.selectedMonth)
//            self.pickerView.reloadComponent(2)
//        case 1://月
//            self.selectedMonth =  row + 1
//
//            self.dayRange = self.isAllDay(year: self.startYear, month: self.selectedMonth)
//            self.pickerView.reloadComponent(2)
//        case 2://日
//            selectedDay = row + 1
//        case 3://时
//            selectedHour = row
//        case 4://分
//            selectedMinute = row
//        case 5://秒
//            selectedSecond = row
//        default:
//            selectedSecond = row
//        }
        
        
    }
    
    //MARK: 获取窗口window
    private func getWindow() ->UIWindow? {
        
        
        if let window:UIWindow = (UIApplication.shared.delegate?.window)! {
            return window
        }else{
            if #available(iOS 13.0, *) {
                let array:Set = UIApplication.shared.connectedScenes
                let windowScene:UIWindowScene = array.first as! UIWindowScene
                if let mainWindow:UIWindow = windowScene.value(forKeyPath:"delegate.window") as? UIWindow{
                    return mainWindow
                }else{
                    return UIApplication.shared.windows.first!
                }
            }else{
                return UIApplication.shared.keyWindow!
            }
        }
    }
    
    class private func getWindow() ->UIWindow? {
        
        return shared.getWindow()
    }
    //MARK: 显示日期选择器
    class func showDatePicker(mode:YYDatePickerMode, title:String?, selectValue:String?, resultBlock:@escaping YYDateResultBlock){
        if shared.superview == nil {
            getWindow()?.addSubview(shared)
        }
        print("currentDatePickerMode = \(mode)")
        shared.titleView.text = title
        shared.initialDateString = selectValue ?? ""
        shared.currentDatePickerMode = mode
        shared.initialDateFormat()
        shared.initData()
        shared.selectResultBlock = resultBlock
    }
    
    class func showDatePicker(datePickerMode:YYDatePickerMode, unitType:YYShowUnitType, title:String?, selectValue:String?, resultBlock:@escaping YYDateResultBlock){
        if shared.superview == nil {
            getWindow()?.addSubview(shared)
        }
        print("currentDatePickerMode = \(datePickerMode)")
        shared.titleView.text = title
        shared.initialDateString = selectValue ?? ""
        shared.currentDatePickerMode = datePickerMode
        shared.currentShowUnitType = unitType
        shared.initialDateFormat()
        shared.initData()
        shared.selectResultBlock = resultBlock
    }
    
    private func initialDateFormat(){
        switch currentDatePickerMode {
        case .YYDatePickerModeDate:
            currentDateFormat = "yyyy-MM-dd"
        case .YYDatePickerModeDateAndTime:
            currentDateFormat = "yyyy-MM-dd HH:mm:ss"
        case .YYDatePickerModeTime:
            currentDateFormat = "HH:mm:ss"
        case .YYDatePickerModeCountDownTimer:
            currentDateFormat = "HH:mm:ss"
        case .YYDatePickerModeYMDHMS:
            currentDateFormat = "yyyy-MM-dd HH:mm:ss"
        case .YYDatePickerModeYMDHM:
            currentDateFormat = "yyyy-MM-dd HH:mm"
        case .YYDatePickerModeYMDH:
            currentDateFormat = "yyyy-MM-dd HH"
        case .YYDatePickerModeMDHM:
            currentDateFormat = "MM-dd HH:mm"
        case .YYDatePickerModeYMD:
            currentDateFormat = "yyyy-MM-dd"
        case .YYDatePickerModeYM:
            currentDateFormat = "yyyy-MM"
        case .YYDatePickerModeY:
            currentDateFormat = "yyyy"
        case .YYDatePickerModeMD:
            currentDateFormat = "MM-dd"
        case .YYDatePickerModeHMS:
            currentDateFormat = "HH:mm:ss"
        case .YYDatePickerModeHM:
            currentDateFormat = "HH:mm"
        case .YYDatePickerModeMS:
            currentDateFormat = "mm:ss"
        }
    }
    
    private func dismissDatePickerView(){
        self.removeFromSuperview()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}































////
////  YYDateTimePickerView.swift
////  DatePickerView
////
////  Created by 蓓蕾 on 2022/3/22.
////
//
//import UIKit
//
//class YYDateTimePickerView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
//
//    let systemBlackColr = UIColor(hex: 0x424243)//系统常用的黑色
//    let systemgrayColor = UIColor(hex: 0xb2b2b2)//系统常用的灰色
//
//    var CancelReturnBlock: (() -> Void)? //取消按钮的回调
//    var SuccessReturnBlock: ((_ date:String) -> Void)?//选择的回调
//
//
//    private lazy var titleView = UILabel()
//    private lazy var cancelButton = UIButton()
//    private lazy var confirmButton = UIButton()
//    var lineView = UIView()//一条横线
//    var pickerView = UIPickerView()
//
//    //数据相关
//    var yearRange = 30 + 1000//年的范围
//    var dayRange = 0 //
//    var startYear = 0//起始年份
//
//    var selectedYear = 0;
//    var selectedMonth = 0;
//    var selectedDay = 0;
//    var selectedHour = 0;
//    var selectedMinute = 0;
//    var selectedSecond = 0;
//
//    var returnString:String  {
//        get{
//            let selectedMonthFormat = String(format:"%.2d",selectedMonth)
//            let selectedDayFormat = String(format:"%.2d",selectedDay)
//            let selectedHourFormat = String(format:"%.2d",selectedHour)
//            let selectedMinuteFormat = String(format:"%.2d",selectedMinute)
//            return "\(selectedYear)-\(selectedMonthFormat)-\(selectedDayFormat) \(selectedHourFormat):\(selectedMinuteFormat)"
//        }
//    } //返回的时间字符串
//
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        initUI()
//        initData()
//    }
//
//
//    convenience init() {
//        self.init(frame:CGRect.init(x: 0, y: 0, width: 100, height: 100))
//        initUI()
//        initData()
//    }
//
//    private func initUI(){
//
//        self.backgroundColor = UIColor.white
//
//        titleView.textColor = systemBlackColr
//        titleView.textAlignment = .center
//        titleView.font = UIFont.systemFont(ofSize: 15)
//        titleView.text = "选择时间"
//        self.addSubview(titleView)
//        titleView.snp.makeConstraints { make in
//            make.centerX.equalToSuperview()
//            make.top.equalToSuperview().offset(15)
//        }
//
//        cancelButton.setTitle("取消", for: .normal)
//        cancelButton.setTitleColor(UIColor.system, for: .normal)
//        cancelButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
//        cancelButton.tag = 101
//        cancelButton.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
//        self.addSubview(cancelButton)
//        cancelButton.snp.makeConstraints { make in
//            make.leading.equalToSuperview().offset(15)
//            make.size.equalTo(CGSize(width: 50, height: 20))
//            make.centerY.equalTo(titleView)
//        }
//
//        lineView.backgroundColor = UIColor.systemGray
//        self.addSubview(lineView)
//        lineView.snp.makeConstraints { make in
//            make.leading.trailing.equalToSuperview()
//            make.height.equalTo(1)
//            make.top.equalTo(cancelButton.snp_bottom).offset(15)
//        }
//
//        confirmButton.setTitle("确定", for: .normal)
//        confirmButton.setTitleColor(UIColor.system, for: .normal)
//        confirmButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
//        confirmButton.tag = 102
//        confirmButton.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
//        self.addSubview(confirmButton)
//        confirmButton.snp.makeConstraints { make in
//            make.size.equalTo(CGSize(width: 50, height: 20))
//            make.centerY.equalTo(titleView)
//            make.trailing.equalToSuperview().offset(-15)
//        }
//
//        pickerView.delegate = self
//        pickerView.dataSource = self
//        self.addSubview(pickerView)
//        pickerView.snp.makeConstraints { make in
//            make.leading.trailing.equalToSuperview()
//            make.top.equalTo(lineView.snp_bottom)
//        }
//
//    }
//
//    //MARK:初始化数据
//    private func initData()  {
//        let  calendar0 = Calendar.init(identifier: .gregorian)//公历
//        var comps = DateComponents()//一个封装了具体年月日、时秒分、周、季度等的类
//        let unitFlags:Set<Calendar.Component> = [.year , .month , .day , .hour , .minute]
//        comps = calendar0.dateComponents(unitFlags, from: Date())
//        startYear = startYear > 0 ? startYear : comps.year! - 100
//        dayRange = isAllDay(year: startYear, month: 1)
//
//        yearRange = 30 + 1000;
//        selectedYear = comps.year!;
//        selectedMonth = comps.month!;
//        selectedDay = comps.day!;
//        selectedHour = comps.hour!;
//        selectedMinute = comps.minute!;
//        self.pickerView.selectRow(selectedYear - startYear, inComponent: 0, animated: true)
//        self.pickerView.selectRow(selectedMonth - 1, inComponent: 1, animated: true)
//        self.pickerView.selectRow(selectedDay - 1, inComponent: 2, animated: true)
//        self.pickerView.selectRow(selectedHour , inComponent: 3, animated: true)
//        self.pickerView.selectRow(selectedMinute , inComponent: 4, animated: true)
//        self.pickerView.reloadAllComponents()
//    }
//
//    //MARK: 计算每个月有多少天
//    func isAllDay(year:Int, month:Int) -> Int {
//        var day:Int = 0
//        switch(month)
//        {
//        case 1,3,5,7,8,10,12:
//            day = 31
//        case 4,6,9,11:
//            day = 30
//        case 2:
//            if(((year%4==0)&&(year%100==0))||(year%400==0))
//            {
//                day=29
//            }
//            else
//            {
//                day=28;
//            }
//        default:
//            break;
//        }
//        return day;
//    }
//
//    @objc private func buttonClick(btn:UIButton){
//        let tag:Int = btn.tag
//        switch tag {
//        case 101:
//            if self.CancelReturnBlock != nil {
//                self.CancelReturnBlock!()
//            }
//        case 102:
//            if self.SuccessReturnBlock != nil {
//                self.SuccessReturnBlock!(returnString)
//            }
//        default:
//            break
//        }
//    }
//
//    //MARK: UIPickerViewDataSource
//
//    //返回UIPickerView当前的列数
//    func numberOfComponents(in pickerView: UIPickerView) -> Int {
//        return 5
//    }
//    //确定每一列返回的东西
//    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//        switch component {
//        case 0:
//            return yearRange
//        case 1:
//            return 12
//        case 2:
//            return dayRange
//        case 3:
//            return 24
//        case 4:
//            return 60
//        default:
//            return 0
//        }
//    }
//    //MARK: UIPickerViewDelegate
//
//    //返回一个视图，用来设置pickerView的每行显示的内容。
//    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
//        let boundsSizeWidth = UIScreen.main.bounds.width
//        let labelView = UILabel(frame: CGRect(x: boundsSizeWidth * CGFloat(component) / 6, y: 0, width: boundsSizeWidth / 6, height: 30))
//        labelView.font = UIFont.systemFont(ofSize: 15)
//        labelView.tag = component*100+row
//        labelView.textAlignment = .center
//        switch component {
//        case 0:
//            labelView.frame=CGRect(x:5, y:0,width:boundsSizeWidth/4.0, height:30);
//            labelView.text="\(self.startYear + row)年";
//        case 1:
//            labelView.frame=CGRect(x:boundsSizeWidth/4.0, y:0,width:boundsSizeWidth/8.0, height:30)
//            labelView.text="\(row + 1)月";
//        case 2:
//            labelView.frame=CGRect(x:boundsSizeWidth*3/8, y:0,width:boundsSizeWidth/8.0, height:30)
//            labelView.text="\(row + 1)日";
//        case 3:
//            labelView.textAlignment = .right
//            labelView.text="\(row )时";
//        case 4:
//            labelView.textAlignment = .right
//            labelView.text="\(row )分";
//        case 5:
//            labelView.textAlignment = .right
//            labelView.frame=CGRect(x:boundsSizeWidth/6, y:0,width:boundsSizeWidth/6.0 - 5, height:30)
//            labelView.text="\(row )秒"
//        default:
//            labelView.text="\(row )秒"
//        }
//        return labelView
//    }
//
//
//    //当点击UIPickerView的某一列中某一行的时候，就会调用这个方法
//    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//        switch component {
//        case 0:
//            self.selectedYear = self.startYear + row
//            self.dayRange = self.isAllDay(year: self.startYear, month: self.selectedMonth)
//            self.pickerView.reloadComponent(2)
//        case 1:
//            self.selectedMonth =  row + 1
//
//            self.dayRange = self.isAllDay(year: self.startYear, month: self.selectedMonth)
//            self.pickerView.reloadComponent(2)
//        case 2:
//            selectedDay = row + 1
//        case 3:
//            selectedHour = row
//        case 4:
//            selectedMinute = row
//        case 5:
//            selectedSecond = row
//        default:
//            selectedSecond = row
//
//        }
//
//
//    }
//
//    //MARK: 获取窗口window
//    private func getWindow() ->UIWindow {
//        //            if let delegate: UIApplicationDelegate = UIApplication.shared.delegate{//UIApplication.sharedApplication().delegate {
//        //                if let window = delegate.window {
//        //                    return window!
//        //                }
//        //            }
//
//        return UIApplication.shared.keyWindow!
//    }
//
//
//    required init?(coder aDecoder: NSCoder) {
//        super.init(coder: aDecoder)
//        fatalError("init(coder:) has not been implemented")
//    }
//
//    /*
//    // Only override draw() if you perform custom drawing.
//    // An empty implementation adversely affects performance during animation.
//    override func draw(_ rect: CGRect) {
//        // Drawing code
//    }
//    */
//
//}
